package cn.edu.jju.psytest.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.edu.jju.psytest.model.entity.Student;
import cn.edu.jju.psytest.model.entity.Teacher;
import cn.edu.jju.psytest.model.entity.User;
import cn.edu.jju.psytest.model.util.EntityVoConverter;
import cn.edu.jju.psytest.model.vo.QueriedStuInfoVo;
import cn.edu.jju.psytest.model.vo.QueriedStuTRVo;
import cn.edu.jju.psytest.model.vo.QueriedTeacherInfoVo;
import cn.edu.jju.psytest.model.vo.QueryStuInfoCriteriaVo;
import cn.edu.jju.psytest.model.vo.QueryStuTRCriteriaVo;
import cn.edu.jju.psytest.model.vo.QueryTeacherCriteriaVo;
import cn.edu.jju.psytest.model.vo.StudentVo;
import cn.edu.jju.psytest.model.vo.TeacherVo;
import cn.edu.jju.psytest.persist.UserDao;

@SuppressWarnings("restriction")
@Service
@Transactional(readOnly = true)
public class UserService {

	private UserDao userDao;

	public User getUser(String userId) {
		return userDao.loadUser(userId);
	}

	@Transactional(readOnly = false)
	public void saveUser(StudentVo studentVo) {
		User user = new User();
		Student student = new Student();
		EntityVoConverter.studentVo2UserEntity(studentVo, user, student);
		userDao.saveUser(user);
		userDao.saveDetailInfoOfStudentUser(student);
	}

	@Transactional(readOnly = false)
	public void saveUser(TeacherVo teacherVo) {
		User user = new User();
		Teacher teacher = new Teacher();
		EntityVoConverter.teacherVo2UserEntity(teacherVo, user, teacher);
		userDao.saveUser(user);
		userDao.saveDetailInfoOfTeacherUser(teacher);
	}
	
	@Transactional(readOnly = false)
	public void deleteTeacherById(String teacherId) {
		userDao.deleteTeacherById(teacherId);
	}

	public Teacher getDetailInfoOfTeacherUser(String teacherId) {
		return userDao.getDetailInfoOfTeacherUser(teacherId);
	}

	public String queryStuTestResultByCriteria(
			QueryStuTRCriteriaVo queryCriteria) {
		List<QueriedStuTRVo> queriedStuTRVos = userDao
				.queryStuTestResultByCriteria(queryCriteria);
		return parseTRObject2JSONString(queriedStuTRVos);
	}

	public String queryStuInfoByCriteria(QueryStuInfoCriteriaVo queryCriteria) {
		List<QueriedStuInfoVo> queriedStuInfoVos = userDao
				.queryStuInfoByCriteria(queryCriteria);
		return parseStuInfoObject2JSONString(queriedStuInfoVos);
	}

	public String queryTeacherInfoByCriteria(
			QueryTeacherCriteriaVo queryCriteria) {
		List<QueriedTeacherInfoVo> queriedTeacherInfoVos = userDao
				.queryTeacherInfoByCriteria(queryCriteria);
		return parseTeacherObject2JSONString(queriedTeacherInfoVos);
	}

	private String parseTRObject2JSONString(List<QueriedStuTRVo> queriedStuTRVos) {
		String json = "";
		for (int i = 0; i < queriedStuTRVos.size(); i++) {
			QueriedStuTRVo queriedUserVo = queriedStuTRVos.get(i);
			json += queriedUserVo.toJSONString() + ",";
		}
		if (json.lastIndexOf(",") != -1) {
			json = json.substring(0, json.lastIndexOf(","));
		}
		return "[" + json + "]";
	}

	private String parseStuInfoObject2JSONString(
			List<QueriedStuInfoVo> queriedStuInfoVos) {
		String json = "";
		for (int i = 0; i < queriedStuInfoVos.size(); i++) {
			QueriedStuInfoVo queriedStuInfoVo = queriedStuInfoVos.get(i);
			json += queriedStuInfoVo.toJSONString() + ",";
		}
		if (json.lastIndexOf(",") != -1) {
			json = json.substring(0, json.lastIndexOf(","));
		}
		return "[" + json + "]";
	}

	private String parseTeacherObject2JSONString(
			List<QueriedTeacherInfoVo> queriedTeacherInfoVos) {
		String json = "";
		for (int i = 0; i < queriedTeacherInfoVos.size(); i++) {
			QueriedTeacherInfoVo queriedTeacherInfoVo = queriedTeacherInfoVos
					.get(i);
			json += queriedTeacherInfoVo.toJSONString() + ",";
		}
		if (json.lastIndexOf(",") != -1) {
			json = json.substring(0, json.lastIndexOf(","));
		}
		return "[" + json + "]";
	}

	/**
	 * Getters And Setters
	 */
	public UserDao getUserDao() {
		return userDao;
	}

	@Resource
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
}
